local AddOnName, Env = ...; local ADDON, DEV = Env[1], Env[1].DEV
-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local LibDBIcon			= LibStub("LibDBIcon-1.0",true)
local AceDB					= LibStub("AceDB-3.0")
local AceDBOptions 		= LibStub("AceDBOptions-3.0")
local AceConfigRegistry	= LibStub("AceConfigRegistry-3.0")
local AceConfigDialog 	= LibStub("AceConfigDialog-3.0")
local LibDataBroker		= LibStub("LibDataBroker-1.1")
local LibDBIcon 			= LibStub("LibDBIcon-1.0",true)
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local select, print						= select, print
local type, tostring, tonumber		= type, tostring, tonumber
local getmetatable						= getmetatable
local sub, find, format, split		= string.sub, string.find, string.format, string.split
local floor 								= math.floor
local remove 								= table.remove
-- ~~| WoW Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local GetSpellInfo, GetTime  			= GetSpellInfo, GetTime
-- ~~| Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- ~~| Event Handlers |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ADDON:UPDATE_BINDINGS(...)
	ADDON.RotationSelecter.HKAutoButton1:SetText( GetBindingKey('CLICK HKAutoButton1:Button4'))
	ADDON.RotationSelecter.HKAutoButton2:SetText( GetBindingKey('CLICK HKAutoButton2:Button4'))
	ADDON.RotationSelecter.HKAutoButton3:SetText( GetBindingKey('CLICK HKAutoButton3:Button4'))
	ADDON.RotationSelecter.HKAutoButton4:SetText( GetBindingKey('CLICK HKAutoButton4:Button4'))
	ADDON.RotationSelecter.HKAutoButton5:SetText( GetBindingKey('CLICK HKAutoButton5:Button4'))
	ADDON.RotationSelecter.HKAutoButton6:SetText( GetBindingKey('CLICK HKAutoButton6:Button4'))
	ADDON.RotationSelecter.HKManualButton1:SetText( GetBindingKey('CLICK HKManualButton1:Button4'))
	ADDON.RotationSelecter.HKManualButton2:SetText( GetBindingKey('CLICK HKManualButton2:Button4'))
	ADDON.RotationSelecter.HKManualButton3:SetText( GetBindingKey('CLICK HKManualButton3:Button4'))
	ADDON.RotationSelecter.HKManualButton4:SetText( GetBindingKey('CLICK HKManualButton4:Button4'))
	ADDON.RotationSelecter.HKManualButton5:SetText( GetBindingKey('CLICK HKManualButton5:Button4'))	
end
-- ~~| ADDON States |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ADDON:OnInitialize()
	-- ~~ Database ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.db = AceDB:New("FireUIDB", self.defaults, true);
	self.db.RegisterCallback(self, "OnProfileChanged", "ProfileUpdate")
	self.db.RegisterCallback(self, "OnProfileCopied", "ProfileUpdate")
	self.db.RegisterCallback(self, "OnProfileReset", "ProfileUpdate")
	-- ~~ Options ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.options.args.profile = AceDBOptions:GetOptionsTable(self.db)
	self.options.args.profile.order = -10
	AceConfigRegistry:RegisterOptionsTable(AddOnName, ADDON.options)
	AceConfigDialog:AddToBlizOptions(AddOnName, nil, nil, "settings")
	AceConfigDialog:AddToBlizOptions(AddOnName, "Profiles", AddOnName, "profile")
	-- ~~ DataBroker Launcher Plugin ~~~~~~~~~~~~~~~~~~~~~~~~
	self.launcher = LibDataBroker:NewDataObject(AddOnName,{
		type 				= "launcher",
		label 			= AddOnName,
		icon 				= ADDON.MEDIAPATH.."LDBIcon",
		OnClick 			= function(this,button)
			ADDON:OpenOptions()
		end,
		OnTooltipShow = function(tooltip)
			tooltip:AddLine(AddOnName, 0, .66, 1)
			tooltip:AddLine(" ")
			tooltip:AddDoubleLine("Click:","Open Config", 0, .66, 1, 1, .83, 0)
		end,
	})
	-- ~~ Update ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	LibDBIcon:Register(AddOnName,self.launcher,self.db.global.minimap)
	-- ~~ Slash commands ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self:RegisterChatCommand('fireui','CommandHandler')
	-- ~~ Construction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.Remote 				= ADDON:constructRemote()	
	self.RotationSelecter 	= ADDON:constructRotationSelecter()
	self.RotationEngine 		= ADDON:constructRotationEngine()	
	self.RotationConfig 		= ADDON:constructRotationConfig()
	self.ActionLog 			= ADDON:constructActionLog()	
	-- ~~ FireHack Check ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	if FireHack then 
		self:OnFireHackLoad() 
	else
		local check = CreateFrame('Frame')	
		check:SetScript('OnUpdate', function(this,elapsed)
			if FireHack then self:OnFireHackLoad() 				
				this:Hide()				
			end	
		end)	
	end 	
	-- ~~ Event Register ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	ADDON:RegisterEvent('UPDATE_BINDINGS')	
	
	self:Update()
	self:Print("v"..ADDON.VERSION.." Loaded.")	
	
end
function ADDON:OnEnable()

end
function ADDON:OnDisable()

end
function ADDON:OnFireHackLoad()
	ADDON.Remote:SetStatus('ready')
	ADDON.RotationSelecter:LoadRotations()
	
	-- TEXPF = TableExplorer:ConstructTableExplorer()
	-- EXPF:SetTable('FireUI',self,2)	
end
-- ~~| ADDON Update |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ADDON:ProfileUpdate()
	self.db = AceDB:New("FireUIDB", self.defaults, true);
	self.db.RegisterCallback(self, "OnProfileChanged", "ProfileUpdate")
	self.db.RegisterCallback(self, "OnProfileCopied", "ProfileUpdate")
	self.db.RegisterCallback(self, "OnProfileReset", "ProfileUpdate")
	-- ~~ Update Database Pointers ~~~~~~~~~~~~~~~~~~~~~~~~~~
	-- Update Database Pointers
	self.Remote.db = ADDON.db.profile.remote
	self.RotationSelecter.db = ADDON.db.profile.rotationSelecter
	self.RotationEngine.db = ADDON.db.profile.rotationEngine
	self.RotationConfig.db = ADDON.db.profile.rotationConfig
	self.ActionLog.db = ADDON.db.profile.actionLog
	-- ~~ Update ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.Remote:ProfileUpdate()	
	self.RotationSelecter:ProfileUpdate()
	
	
	self:Update()
end
function ADDON:Update()
	if not ADDON:IsEnabled() then return false end

	if self.db.global.minimap.hide then	LibDBIcon:Hide(AddOnName) else LibDBIcon:Show(AddOnName) end

	self.Remote:Update()
	self.RotationSelecter:Update()
	self.RotationConfig:Update()
	self.ActionLog:Update()
end

